home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c++ / 410 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  2.9 KB

  1. Path: chronicle.mti.sgi.com!austern
  2. From: Kevlin A P Henney <Kevlin@two-sdg.demon.co.uk>
  3. Newsgroups: comp.std.c++
  4. Subject: Re: Numeric literal formats
  5. Date: 20 Feb 1996 10:55:25 PST
  6. Organization: 2sdg Ltd
  7. Approved: austern@isolde.mti.sgi.com
  8. Message-ID: <824739573snz@two-sdg.demon.co.uk>
  9. References: <3125C1DC.35AE@ix.netcom.com>
  10. Reply-To: kevlin@two-sdg.demon.co.uk
  11. NNTP-Posting-Host: isolde.mti.sgi.com
  12. X-Original-Date: Mon, 19 Feb 96 14:19:33 GMT
  13. X-Newsreader: Demon Internet Simple News v1.29
  14. X-Mail2News-Path: two-sdg.demon.co.uk
  15. X-Auth: PGPMoose V1.1 PGP comp.std.c++
  16.     iQBVAwUBMSoZL0y4NqrwXLNJAQF0LwH+IzgCcJcvAfslhcdEX+/0Jb67woXJwLvp
  17.     RunnmC0iBijB09VXRGMKXWuK3E1vBVu/2LD8bQN04rfCSKcF5hGFUw==
  18.     =iRFG
  19. Originator: austern@isolde.mti.sgi.com
  20.  
  21. In article <3125C1DC.35AE@ix.netcom.com>
  22.            pderocco@ix.netcom.com "Paul D. DeRocco" writes:
  23.  
  24. > Why, oh why, aren't we allowed to write binary numbers in C++? The 
  25. > standard could trivially be extended by allowing binary numbers to be 
  26. > written, prefixed with 0b, or perhaps 0y. This makes bit masks _much_ 
  27. > easier to read. And it wouldn't break any presently legal program.
  28.  
  29. You might like to try the following:
  30.  
  31.     template<int digit> struct bit;
  32.  
  33.     struct bit<0> { enum { value = 0 }; };
  34.  
  35.     struct bit<1> { enum { value = 1 }; };
  36.  
  37.     template<int digits> struct bin;
  38.  
  39.     struct bin<0> { enum { value = 0 }; };
  40.  
  41.     template<int digits> struct bin
  42.     {
  43.         enum
  44.         {
  45.             value =    bin<digits / 10>::value * 2 +
  46.                 bit<digits % 10>::value
  47.         };
  48.     };
  49.  
  50. Used as
  51.  
  52.     bin<1101>::value
  53.     bin<11001100>::value
  54.  
  55. Catches as illegal
  56.  
  57.     bin<12345>::value
  58.     bin<011001>::value
  59.  
  60. If nothing else, the code is a good source of amusement -- present it to your
  61. colleagues w/ the names changed to perplex the innocent :-)
  62.  
  63. Seriously, this is practical w/i the constraints of 32 bit ints and <= 10 bit
  64. bit specs, and for compilers using the old specialization syntax (ie. the
  65. majority).
  66.  
  67. > Another related improvement would be to allow embedded underscores in 
  68. > numbers, for clarity. 1_000_000_000 is obviously a billion, while 
  69. > 1000000000 is not so obviously a billion. Same for 0x8000_0000, or 
  70. > 0y0001_1110_0000_0000. Again, it doesn't break anything.
  71.  
  72. Alternatively we could try for space separation and handle adjacent literals
  73. as one, as is done w/ string literals:
  74.  
  75.     1 000 000 000
  76.     0x 8000 0000
  77.     0.12345 67890
  78.  
  79. :->
  80. _______________________________________________________________________
  81.  
  82.   Kevlin A P Henney        2sdg Ltd        kevlin@two-sdg.demon.co.uk
  83.   "What happens to the hole when the cheese is gone?"  Bertolt Brecht
  84. _______________________________________________________________________
  85. ---
  86. [ To submit articles: Try just posting with your newsreader.  If that fails,
  87.                       use mailto:std-c++@ncar.ucar.edu
  88.   FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html
  89.   Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html
  90.   Comments? mailto:std.c++-request@ncar.ucar.edu 
  91. ]
  92.